﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JYH.Framework;
using System.Data;
using System.IO;
using System.Xml;

namespace JYH.Framework
{
    public class RStateHelp
    {

        /// <summary>
        /// 获取状态信息字典,字典中Key=后台数值，字典中Value=显示文本
        /// </summary>
        /// <param name="Key">XML标识,可以为空</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetRState(string Key)
        {
            if (string.IsNullOrEmpty(Key))
                Key = "TY";
            string fileConfig = System.Threading.Thread.GetDomain().SetupInformation.ConfigurationFile;
            string filePath = Path.GetDirectoryName(fileConfig);
            string fileName = filePath + @"\" + "TypeState.xml";
            Dictionary<string, string> list = new Dictionary<string, string>();
            if (!File.Exists(fileName))
            {
                 fileName = filePath + @"\Bin\" + "TypeState.xml";
            }
            XmlNodeList nodes = XMLHelper.ReadNodes(fileName, "TypeState");
            if (nodes.Count == 1)
            {
                XmlNodeList items = nodes[0].ChildNodes;
                foreach (XmlNode item in items)
                {
                    if(!list.ContainsKey(item.Attributes["Code"].Value))
                       list.Add(item.Attributes["Code"].Value, item.Attributes["Desc"].Value);
                }
            }
            else
            {
                XmlNodeList itemBase  =nodes[0].ChildNodes; ;
                foreach (XmlNode node in nodes)
                {
                    if (string.Equals(node.Attributes["Key"].Value, Key))
                    {
                        XmlNodeList items = node.ChildNodes;
                        foreach (XmlNode item in items)
                        {
                            if (!list.ContainsKey(item.Attributes["Code"].Value))
                                list.Add(item.Attributes["Code"].Value, item.Attributes["Desc"].Value);
                        }
                    }
                    if (string.Equals(node.Attributes["Key"].Value,"TY"))
                    {
                        itemBase = node.ChildNodes;
                    }
                   
                }
                foreach (XmlNode item in itemBase)
                {
                    if (!list.ContainsKey(item.Attributes["Code"].Value))
                        list.Add(item.Attributes["Code"].Value, item.Attributes["Desc"].Value);
                }
            }
            
            return list;
        }
        /// <summary>
        /// 获取状态信息
        /// </summary>
        /// <param name="Key">XML标识,可以为空</param>
        /// <param name="Code">后台数值标识</param>
        /// <returns></returns>
        public static string GetRStateName(string Key, string Code)
        {
            string CodeName = string.Empty;
            Dictionary<string, string> list = GetRState(Key);
            list.TryGetValue(Code, out CodeName);
            return CodeName;
        }
        /// <summary>
        /// 获取状态信息数据表(DataTable),显示文本对应字段CName,后台数值对应字段CValue
        /// </summary>
        /// <param name="Key">XML标识</param>
        /// <returns></returns>
        public static DataTable GetRStateTable(string Key)
        {
            DataTable dt = new DataTable();
            Dictionary<string, string> list = GetRState(Key);
            dt = DataTableHelper. GetDataTableFromDictionary(list);
            return dt;
        }
    }
}
